package com.wwdx.service.impl;

import com.google.common.collect.Maps;
import com.wwdx.model.WwdxPersonalModel;
import com.wwdx.model.WwdxXiaodModel;
import com.wwdx.service.WwdxXiaodServiceI;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author liguoliang
 */
@Service
public class WwdxXiaodServiceImpl extends BaseServiceImpl<WwdxXiaodModel> implements WwdxXiaodServiceI {

    @Override
    public Long selectModelCount() {
        String sql = "SELECT COUNT(1) FROM (SELECT COUNT(1) FROM wwdx_xiaod WHERE type = 1 GROUP BY userid) as t";
        return this.countBySql(sql).longValue();
    }

    @Override
    public List<WwdxXiaodModel> selectModelList(Integer pageIndex, Integer pageSize) {
        String sql = "select * from wwdx_xiaod where id in(\n" +
                    "select max(id) as id  from wwdx_xiaod \n" +
                    "where type=1 group by userid) order by gmt_created desc";
        List<WwdxXiaodModel> wwdxXiaodModels = this.findBySqlToListBean(sql, WwdxXiaodModel.class, pageIndex, pageSize);
        wwdxXiaodModels.forEach(wwdxXiaodModel->{
            Long userid = wwdxXiaodModel.getUserid();
            WwdxPersonalModel wwdxPersonalModel = wwdxPersonalService.getById(userid);
            if (wwdxPersonalModel != null){
                wwdxXiaodModel.setNickname(wwdxPersonalModel.getNickname());
            }
        });
        return wwdxXiaodModels;
    }

    @Override
    public void modifyMessageByUserId(Long userid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("status",1);
        params.put("userid",userid);
        String sql = "UPDATE wwdx_xiaod SET status=:status where userid=:userid";
        this.executeSql(sql,params);
    }

    @Override
    public List<WwdxXiaodModel> selectModelList(Long userid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("userid",userid);
        List<WwdxXiaodModel> wwdxXiaodModels = this.find("from WwdxXiaodModel where userid=:userid",params);
        wwdxXiaodModels.forEach(wwdxXiaodModel -> {
            wwdxXiaodModel.setNickname(wwdxPersonalService.getById(wwdxXiaodModel.getUserid()).getNickname());
            wwdxXiaodModel.setXnickname("读秀");
        });
        return wwdxXiaodModels;
    }

    @Override
    public Long selectModelCount(Long userid) {
        Map<String, Object> params = Maps.newHashMapWithExpectedSize(2);
        params.put("userid",userid);
        String hql = "select count(1) from WwdxXiaodModel where userid=:userid";
        return this.count(hql, params);
    }
}